package com.xizi.lotterySystem.dao.mapper;

import com.xizi.lotterySystem.dao.handler.Encrypt;
import com.xizi.lotterySystem.dao.dataobject.UserDO;
import com.xizi.lotterySystem.service.dto.UserDTO;
import com.xizi.lotterySystem.service.dto.UserLoginDTO;
import com.xizi.lotterySystem.service.dto.UserRegisterDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Insert("insert into user (user_name,email,phone_number,password,salt,`identity`)  " +
            " values (#{userName},#{email},#{phoneNumber},#{password},#{salt},#{identity})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    int insertUser(UserRegisterDTO userRegisterDTO);

    @Select("select count(*) from user where email = #{email}")
    int selectCountByEmail(String email);

    @Select("select count(*) from user where phone_number = #{phoneNumber}")
    int selectCountByPhoneNumber(Encrypt phoneNumber);

    @Select("select user_name,email,phone_number,password,salt,`identity` from user " +
            " where phone_number = #{phoneNumber}")
    UserLoginDTO selectByPhoneNumber(Encrypt phoneNumber);

    @Select("select user_name,email,phone_number,password,salt,`identity` from user " +
            " where email = #{email}")
    UserLoginDTO selectByEmail(String email);

    @Select("<script>" +
            "SELECT * FROM user " +
            "<where> " +
            "   <if test='identity != null'> " +
            "       identity = #{identity} " +
            "   </if> " +
            "</where> " +
            "</script>")
    List<UserDTO> selectAllByIdentity(@Param("identity") String identity);

    @Select("<script>" +
            "SELECT * FROM user " +
            " where id in " +
            " <foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            " #{id}" +
            " </foreach>" +
            "</script>")
    List<UserDO> selectByIds(List<Long> ids);
}
